package com.wh.message.jpush.service.impl;

import com.wh.common.util.Result;
import com.wh.message.jpush.in.JPushMessageIn;
import com.wh.message.jpush.service.JPushMessageService;
import com.wh.message.jpush.utils.JPushUtils;
import com.wh.message.utils.ValidatorFactoryUtil;
import com.wh.msg.ResultCode;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;


/**
 * 极光推送服务实现层
 * @author 谢泽鹏
 * @version V1.0
 * @Package com.wh.message.mq.service.impl
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
public class JPushMessageServiceImpl implements JPushMessageService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 推送服务是否开启
     */
    private String JPUSH_OPEN;

    /**
     * 激光应用MasterSecret
     */
    private  String MASTER_SECTET;

    /**
     * 激光应用AppKey
     */
    private String APP_KEY;

    /**
     *
     */
    private String PARAM_PREFIX;


    /**
     * 消息推送 通知模式
     * @param jPushMessageIn 推送入参
     * @return
     */
    @Override
    public Result sendJPush(JPushMessageIn jPushMessageIn){
        logger.info("sendJPush消息推送-->>开始");
        logger.info("sendJPush消息推送jpushMessageIn = {}", JSONObject.fromObject(jPushMessageIn).toString());
        Result result=new Result();
        try {
            result= ValidatorFactoryUtil.validate(jPushMessageIn);
            if(!ResultCode.RES_OK.equals(result.getResCode())){
                logger.info("sendJPush消息推送，参数校验不通过,result = {}",result);
                return result;
            }
            Map<String,String> nepmap = new HashMap<String, String>();
            boolean falg = JPushUtils.pushNotifiton(jPushMessageIn.getUserId(), jPushMessageIn.getContent(), nepmap, jPushMessageIn.getTitle(), MASTER_SECTET, APP_KEY, jPushMessageIn.getRowNum(), Boolean.parseBoolean(JPUSH_OPEN), PARAM_PREFIX);
            if(falg){
                logger.info("sendJPush消息推送-->>成功");
                result.setResCode(ResultCode.RES_OK);
                return result;
            }
            else{
                logger.info("sendJPush消息推送-->>失败");
                result.setResCode(ResultCode.RES_NO);
                return result;
            }
        }catch (Exception e){
            logger.error("sendJPush消息推送出现异常msg = {}", e.getMessage());
            e.printStackTrace();
            result.setResCode(ResultCode.RES_ERR);
            result.setResult(e.getMessage());
        }
        logger.info("sendJPush消息推送-->>结束");
        return result;
    }

    public String getMASTER_SECTET() {
        return MASTER_SECTET;
    }

    public void setMASTER_SECTET(String MASTER_SECTET) {
        this.MASTER_SECTET = MASTER_SECTET;
    }

    public String getAPP_KEY() {
        return APP_KEY;
    }

    public void setAPP_KEY(String APP_KEY) {
        this.APP_KEY = APP_KEY;
    }

    public String getJPUSH_OPEN() {
        return JPUSH_OPEN;
    }

    public void setJPUSH_OPEN(String JPUSH_OPEN) {
        this.JPUSH_OPEN = JPUSH_OPEN;
    }

    public String getPARAM_PREFIX() {
        return PARAM_PREFIX;
    }

    public void setPARAM_PREFIX(String PARAM_PREFIX) {
        this.PARAM_PREFIX = PARAM_PREFIX;
    }
}
